package com.study.controller;

import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;
import java.util.UUID;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.io.FilenameUtils;
import org.aspectj.weaver.ast.And;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.study.Application;
import com.study.model.pojo.Category;
import com.study.model.pojo.Dictionary;
import com.study.model.pojo.Good;
import com.study.services.CategoryService;
import com.study.services.DictionaryService;
import com.study.services.GoodsService;
import com.study.services.OrderService;

@Controller
public class GoodsController {
	@Autowired
	GoodsService service;
	@Autowired
	DictionaryService dis;
	@Autowired
	CategoryService cs;
	@Autowired
	OrderService orderService;
	
	/**
	 * 跳转高级查询
	 * @return
	 */
	@RequestMapping("/morequery")
	public ModelAndView more() {
		List<JSONObject> list = service.queryAll();
		ModelAndView mv = new ModelAndView("/goods/moreQuery");
		mv.addObject("list", list);
		return mv;
	}
	
	/**
	 *	高级查询
	 * @param currentPage
	 * @param pagesize
	 * @param gname
	 * @param catname
	 * @param gid
	 * @param status
	 * @param xprice
	 * @param dprice
	 * @return 
	 * @return
	 */
	@PostMapping("/superQuery")
	@ResponseBody
	public String superQuery(Integer currentPage,Integer pagesize,String gname,String catid,String gid,
			String gstatus,String xprice,String dprice,String xnum,String dnum) {
		System.out.println("品名："+gname+"\n类名："+catid+"\n编号："+gid+"\n状态："+gstatus+"\n价格1："+xprice+"\n价格2："+dprice);
		List<JSONObject> list = service.more(currentPage, pagesize, gname, catid, gid, gstatus, xprice, dprice,xnum,dnum);
		System.out.println(list);
		PageInfo<JSONObject> pager = new PageInfo<JSONObject>(list);
		String json = JSONObject.toJSONString(pager);
		System.out.println(json);
		return json;
	}
	
	

	/**
	 * 查询
	 * @return
	 */
	@PostMapping("/glist")
	@ResponseBody
	public String selectByPage(Integer currentPage,Integer pagesize,String gname,String catid,String gid,
			String gstatus,String xprice,String dprice,String xnum,String dnum) {
		//List<JSONObject> list = service.selectBySel(1, 10, "", "","");
		System.out.println("品名："+gname+"\n类名："+catid+"\n编号："+gid+"\n状态："+gstatus+"\n价格1："+xprice+"\n价格2："+dprice);
		List<JSONObject> list = service.more(1, 10, gname, catid, gid, gstatus, xprice, dprice,xnum,dnum);
		PageInfo<JSONObject> pager = new PageInfo<JSONObject>(list);
        String json = JSONObject.toJSONString(pager);
		System.out.println(json);
		return json;
	}
	
	@GetMapping("/glist")
	@ResponseBody
	public ModelAndView getForm4() {
		ModelAndView mv = new ModelAndView("/goods/productList");
		return mv;
	}
	
	/**
	 * 带条件查询
	 * @return
	 */
	@PostMapping("/pagelist")
	@ResponseBody
	public String getForm3(String cat_name,String input,
			String selects,
			int currentPage,
			int pagesize,
			HttpSession session,HttpServletRequest request) {
		System.out.println(selects+"===="+input+"====="+cat_name);
		List<JSONObject> list = service.selectBySel(currentPage, pagesize,cat_name, input, selects);
		System.err.println(list);
		PageInfo<JSONObject> pager = new PageInfo<JSONObject>(list);
		String json = JSONObject.toJSONString(pager);
		return json;
	}
	
	@RequestMapping("/goodAdd")
	public ModelAndView add() {
		ModelAndView mv = new ModelAndView("/goods/productAdd");
		mv.addObject("good", new JSONObject());
		return mv;
	}
	
	/**
	 * 商品详情页
	 * @param request
	 * @return
	 */
	@GetMapping("/productdesc")
	public ModelAndView desc(HttpServletRequest request) {
		String goo_id = request.getParameter("id");
		System.out.println("进来没有"+goo_id);
		JSONObject good = service.findById(goo_id);
		System.out.println(good);
		ModelAndView mv = new ModelAndView("/goods/productdesc");
		mv.addObject("good",good);
		return mv;
	}
	
	/**
	 * 删除商品
	 * @param goo_id
	 * @return
	 */
	@PostMapping("/delgood")
	@ResponseBody
	public String delete(Integer goo_id) {
		System.out.println(goo_id);
		service.delete(goo_id);
		return "/glist";
	}
	
	/**
	 * 新增商品信息
	 * @param gname
	 * @param catid
	 * @param gid
	 * @param gstatus
	 * @param gspec
	 * @param gunit
	 * @param gprice
	 * @param gimg
	 * @return
	 */
	@PostMapping("/addgood")
	@ResponseBody
	public ModelAndView addGoods(String gname,@RequestParam("catid[]") int[] catid,Integer gid,Integer gstatus,
						Integer gunit,BigDecimal gprice,Integer ddtime,
						HttpServletRequest request) {
		HttpSession session = request.getSession();
		String gimg  = (String) session.getAttribute("gooImg");
		Dictionary d = dis.findByDicId(gstatus);
		Dictionary d2 = dis.findByDicId(gunit);
		Category c = cs.findById(catid[catid.length-1]);
		Good g = new Good();
		g.setGooName(gname);
		g.setCategory(c);
		g.setDictionary1(d2);
		g.setDictionary2(d);
		g.setGooPrice(gprice);
		g.setGooSpec(0);
		g.setGdel(1);
		g.setDdtime(ddtime);
		if (gimg==null) {
			g.setGooImg("/gimg/nullimg.png");
		}else {
			g.setGooImg(gimg);
		}
		service.insert(g);
		ModelAndView mv = new ModelAndView("/goods/productList");
		//查询商品树形数据
		session.getServletContext().removeAttribute("treeitems");
		JSONArray findByTree = orderService.findByTree();
		session.getServletContext().setAttribute("treeitems",findByTree);
		return mv;
	}
	
	/**
	 * 编辑完提交
	 * @param gname
	 * @param catid
	 * @param gid
	 * @param gstatus
	 * @param gspec
	 * @param gunit
	 * @param gprice
	 * @param gimg
	 * @param id
	 * @return
	 */
	@PostMapping("/editgoodForm")
	@ResponseBody
	public String editGoods(String gname,@RequestParam("catid[]") int[] catid,Integer gid,Integer gstatus,
						Integer gspec,Integer gunit,BigDecimal gprice,String gimg,Integer ddtime,
						   String id,HttpSession session) {
		System.out.println(id+"编辑商品信息");
		boolean result = service.editgood(gname, catid, gid, gstatus, gspec, gunit, gprice, gimg,ddtime, id);
		System.out.println(result+"编辑结果");
		if (result) {
			return "ok";
		}else {
			return "fail";
		}
		
	}
	
	/**
	 * 跳转编辑
	 * @param request
	 * @return
	 */
	@GetMapping("/goodEditor")
	public ModelAndView updateGood(HttpServletRequest request) {
		String goo_id = request.getParameter("id");
		JSONObject good = service.findById(goo_id);
		good.put("zilei", Integer.parseInt(good.getString("cat_id")));
		Category ca = cs.findById(Integer.parseInt(good.getString("cat_id")));
		good.put("fulei", ca.getPid());
		ModelAndView mv = new ModelAndView("/goods/productAdd");
		mv.addObject("good",good);
		System.out.println("可以编辑吗"+goo_id);
		return mv;
	}
	
}
